Skip to content

Normalize if/else blocks#10270

Draft
niloc132 wants to merge 23 commits into
gwtproject:mainfrom
niloc132:10239-if-child-blocks-test
Draft

Normalize if/else blocks#10270
niloc132 wants to merge 23 commits into
gwtproject:mainfrom
niloc132:10239-if-child-blocks-test

Conversation

@niloc132

@niloc132 niloc132 commented Feb 4, 2026

Copy link
Copy Markdown
Member

Fixes a longstanding TODO, and makes it easier for visitors to rewrite contents of blocks in if statements. Originally this change was intended to have no impact on generated JS, but resulted in a dangling-else problem, so changes were required to emit blocks when an if has an else.

Several tests needed to be updated to no longer assume that an unnecessary block was present after parsing.

Fix #10239

@niloc132 niloc132 added this to the 2.14 milestone Feb 4, 2026
@niloc132

niloc132 commented Feb 4, 2026

Copy link
Copy Markdown
Member Author

As of this comment, samples are at 10807627 from 10821121, 13494 bytes saved, about 0.1% smaller.

I'm skeptical about making the new method on JBlock - though it should be used for for/while/do-while blocks as well (though not try blocks). Probably should resolve that one way or the other before merge.

The change to printing blocks in JS when an if has an else are more aggressive than they need to be - this need not happen unless the if has a child if, so we could optimize very slightly more here. Options here include keeping nested blocks for ifs manual and introducing a normalization pass rather than a feature of the JsToStringGenerator, or adding a visitor when printing the JS to see if the block is needed.

Java 17 change was done to ease test writing, and probably should be done anyway as part of the 2.14 release process.

#10240 was also addressed then reverted in this branch, to follow in a later PR.

Comment on lines +131 to +132
if (jsStatement instanceof JsExprStmt && ((JsExprStmt) jsStatement).getExpression() instanceof JsFunction) {
JsFunction jsFunction = (JsFunction) ((JsExprStmt) jsStatement).getExpression();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (jsStatement instanceof JsExprStmt && ((JsExprStmt) jsStatement).getExpression() instanceof JsFunction) {
JsFunction jsFunction = (JsFunction) ((JsExprStmt) jsStatement).getExpression();
if (jsStatement instanceof JsExprStmt exprStmt
&& exprStmt.getExpression() instanceof JsFunction jsFunction) {

If we're using Java 17 here, we can simplify the casts a bit.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed - the change to 17 was mostly for tests, but it could benefit the code in other places too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Normalize then/else to always be blocks in Java AST

2 participants